home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / ibus / lookuptable.py < prev    next >
Text File  |  2009-11-05  |  7KB  |  232 lines

  1. # vim:set et sts=4 sw=4:
  2. #
  3. # ibus - The Input Bus
  4. #
  5. # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this program; if not, write to the
  19. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20. # Boston, MA  02111-1307  USA
  21.  
  22. __all__ = (
  23.         "LookupTable",
  24.     )
  25.  
  26. import dbus
  27. from serializable import *
  28. from exception import *
  29.  
  30. class LookupTable(Serializable):
  31.     __gtype_name__ = "PYIBusLookupTable"
  32.     __NAME__ = "IBusLookupTable"
  33.     def __init__(self, page_size=5, cursor_pos=0, coursor_visible=True, round=False, candidates=None, labels=None):
  34.         super(LookupTable, self).__init__()
  35.         self.__cursor_pos = cursor_pos
  36.         self.__cursor_visible = True
  37.         self.__round = round
  38.         if candidates == None:
  39.             self.__candidates = list()
  40.         else:
  41.             self.__candidates = candidates
  42.         self.set_page_size(page_size)
  43.         self.set_labels(labels)
  44.  
  45.     def set_page_size(self, page_size):
  46.         self.__page_size = page_size
  47.  
  48.     def get_page_size(self):
  49.         return self.__page_size
  50.  
  51.     def get_current_page_size(self):
  52.         nr_candidate = len(self.__candidates)
  53.         nr_page, last_page_size = divmod(nr_candidate, self.__page_size)
  54.         if self.__cursor_pos / self.__page_size == nr_page:
  55.             return last_page_size
  56.         else:
  57.             return self.__page_size
  58.  
  59.     def set_labels(self, labels):
  60.         if labels == None:
  61.             self.__labels = list()
  62.         else:
  63.             self.__labels = labels
  64.  
  65.     def get_labels(self):
  66.         return self.__labels
  67.  
  68.     def show_cursor(self, show = True):
  69.         self.__cursor_visible = show
  70.  
  71.     def is_cursor_visible(self):
  72.         return self.__cursor_visible
  73.  
  74.     def get_current_page_start(self):
  75.         return (self.__cursor_pos / self.__page_size) * self.__page_size
  76.  
  77.     def set_cursor_pos(self, pos):
  78.         if pos >= len(self.__candidates) or pos < 0:
  79.             return False
  80.         self.__cursor_pos = pos
  81.         return True
  82.  
  83.     def get_cursor_pos(self):
  84.         return self.__cursor_pos
  85.  
  86.     def get_cursor_pos_in_current_page(self):
  87.         page, pos_in_page = divmod(self.__cursor_pos, self.__page_size)
  88.         return pos_in_page
  89.  
  90.     def set_cursor_pos_in_current_page(self, pos):
  91.         if pos < 0 or pos >= self.__page_size:
  92.             return False
  93.         pos += self.get_current_page_start()
  94.         if pos >= len(self.__candidates):
  95.             return False
  96.         self.__cursor_pos = pos
  97.         return True
  98.  
  99.  
  100.     def page_up(self):
  101.         if self.__cursor_pos < self.__page_size:
  102.             if self.__round:
  103.                 nr_candidates = len(self.__candidates)
  104.                 max_page = nr_candidates / self.__page_size
  105.                 self.__cursor_pos += max_page * self.__page_size
  106.                 if self.__cursor_pos > nr_candidates - 1:
  107.                     self.__cursor_pos = nr_candidates - 1
  108.                 return True
  109.             else:
  110.                 return False
  111.  
  112.         self.__cursor_pos -= self.__page_size
  113.         return True
  114.  
  115.     def page_down(self):
  116.         current_page = self.__cursor_pos / self.__page_size
  117.         nr_candidates = len(self.__candidates)
  118.         max_page = nr_candidates / self.__page_size
  119.  
  120.         if current_page >= max_page:
  121.             if self.__round:
  122.                 self.__cursor_pos %= self.__page_size
  123.                 return True
  124.             else:
  125.                 return False
  126.  
  127.         pos = self.__cursor_pos + self.__page_size
  128.         if pos >= nr_candidates:
  129.             pos = nr_candidates - 1
  130.         self.__cursor_pos = pos
  131.  
  132.         return True
  133.  
  134.     def cursor_up(self):
  135.         if self.__cursor_pos == 0:
  136.             if self.__round:
  137.                 self.__cursor_pos = len(self.__candidates) - 1
  138.                 return True
  139.             else:
  140.                 return False
  141.  
  142.         self.__cursor_pos -= 1
  143.         return True
  144.  
  145.     def cursor_down(self):
  146.         if self.__cursor_pos == len(self.__candidates) - 1:
  147.             if self.__round:
  148.                 self.__cursor_pos = 0
  149.                 return True
  150.             else:
  151.                 return False
  152.  
  153.         self.__cursor_pos += 1
  154.         return True
  155.  
  156.     def clean(self):
  157.         self.__candidates = list()
  158.         self.__cursor_pos = 0
  159.  
  160.     def append_candidate(self, text):
  161.         self.__candidates.append(text)
  162.  
  163.     def get_candidate(self, index):
  164.         return self.__candidates[index]
  165.  
  166.     def append_label(self, text):
  167.         self.__labels.append(text)
  168.  
  169.     def get_label(self, index):
  170.         return self.__labels[index]
  171.  
  172.     def get_candidates_in_current_page(self):
  173.         page = self.__cursor_pos / self.__page_size
  174.         start_index = page * self.__page_size
  175.         end_index = min((page + 1) * self.__page_size, len(self.__candidates))
  176.         return self.__candidates[start_index:end_index]
  177.  
  178.     def get_current_candidate(self):
  179.         return self.__candidates [self.__cursor_pos]
  180.  
  181.     def get_number_of_candidates(self):
  182.         return len(self.__candidates)
  183.  
  184.     def __len__(self):
  185.         return self.get_number_of_candidates()
  186.  
  187.     def serialize(self, struct):
  188.         super(LookupTable, self).serialize(struct)
  189.         struct.append(dbus.UInt32(self.__page_size))
  190.         struct.append(dbus.UInt32(self.__cursor_pos))
  191.         struct.append(dbus.Boolean(self.__cursor_visible))
  192.         struct.append(dbus.Boolean(self.__round))
  193.         candidates = map(lambda c: serialize_object(c), self.__candidates)
  194.         struct.append(dbus.Array(candidates, signature="v"))
  195.         labels = map(lambda c: serialize_object(c), self.__labels)
  196.         struct.append(dbus.Array(labels, signature="v"))
  197.  
  198.  
  199.     def get_current_page_as_lookup_table(self):
  200.         candidates = self.get_candidates_in_current_page()
  201.         return LookupTable(self.__page_size,
  202.                            self.__cursor_pos % self.__page_size,
  203.                            self.__cursor_visible,
  204.                            self.__round,
  205.                            candidates,
  206.                            self.__labels)
  207.  
  208.     def deserialize(self, struct):
  209.         super(LookupTable, self).deserialize(struct)
  210.  
  211.         self.__page_size = struct.pop(0)
  212.         self.__cursor_pos = struct.pop(0)
  213.         self.__cursor_visible = struct.pop(0)
  214.         self.__round = struct.pop(0)
  215.         self.__candidates = map(deserialize_object, struct.pop(0))
  216.         self.__labels = map(deserialize_object, struct.pop(0))
  217.  
  218. def test():
  219.     t = LookupTable()
  220.     # attrs = AttrList()
  221.     # attrs.append(AttributeBackground(RGB(233, 0,1), 0, 3))
  222.     # attrs.append(AttributeUnderline(1, 3, 5))
  223.     t.append_candidate("Hello")
  224.     value = serialize_object(t)
  225.     t = deserialize_object(value)
  226.     t = t.get_current_page_as_lookup_table()
  227.     value = serialize_object(t)
  228.     t = deserialize_object(value)
  229.  
  230. if __name__ == "__main__":
  231.     test()
  232.